package com.kk.service.impl;

import com.aliyuncs.utils.ParameterHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kk.Entry.Employee;
import com.kk.Mapper.EmpMapper;
import com.kk.dto.EmpDto;
import com.kk.service.EmpService;
import com.kk.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public Map<String, Object> findPageEmp(EmpDto dto) {
        /*思路
        * new一个hashMap<String,object>然后调用工具类pageHelper。startPage方法把页码过去
        * 调用mapper的查询方法 得到emp集合的返回值然后需要把empList封装成PageInfo
        * 再拿最开始定义的空HashMap往里面装键对 （值是封装后的PageInfo.get*)
        * */
        Map<String, Object> map = new HashMap<String, Object>();
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Employee> employeeList = empMapper.findPageEmp(dto);
        PageInfo<Employee> employeePageInfo = PageInfo.of(employeeList);
        map.put("total", employeePageInfo.getTotal());
        map.put("rows", employeePageInfo.getList());
        return map;
    }

    @Override
    public int deleteEmps(ArrayList<Integer> ids) {
        int i = empMapper.deleteEmps(ids);
        return i;
    }

    @Override
    public int addEmp(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());
        return empMapper.addEmp(employee);
    }

    @Override
    public Employee findById(Integer id) {
        return empMapper.findById(id);
    }

    @Override
    public int updateEmp(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        return empMapper.updateEmp(employee);
    }

    @Override
    public String loginEmp(Employee employee) {
        Employee res = empMapper.loginEmp(employee);
        if (res != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("username", res.getUsername());
            map.put("password", res.getPassword());
            return JwtUtils.getJwt(map);
        }
        return null;

    }
}
